//
//  JDOrderTableViewCell.swift
//  DragonWallet
//
//  Created by volantPig on 2024/5/11.
//

import UIKit
import SwiftyJSON

class JDOrderTableViewCell: UITableViewCell {

    var orderType: JDOrderType = .waitingPay
    
    var containerView: UIView?
    
    var storeNameLabel: UILabel?
    var statusLabel: UILabel?
    var statusImgV: UIImageView?
    
//    var goodsOneView: JDOrderGoodsView?
//    var goodsTwoView: JDOrderGoodsView?
    
    var goodsTableView: UITableView?

    var totalStr: UILabel?
    var orderPriceLabel: UILabel?
    var orderPriceTip: UILabel?
    var tagLabel: UILabel?
    
    var waittingPayHintLabel: JDPaddingLabel?
    var bottomLine: UIView?// 控制这个距离底部的高度
    
    var bottomFunctionView: UIView?
    var moreButton: UIButton?
    var cancelButton: UIButton?
    var payButton: UIButton?
    
    var topLine: UIView?
    
    var orderModel: JDOrderListModel? {
        didSet {
            self.refreshData()
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.backgroundColor = color("#F5F6F8")
        for subV in self.contentView.subviews {
            subV.removeFromSuperview()
        }
        self.setupUIElements()
    }
    
    func setupUIElements() {
        
        self.containerView = UIView()
        self.contentView.addSubview(self.containerView!)
        self.containerView?.snp.makeConstraints({ make in
            make.left.equalTo(self.contentView).offset(12)
            make.right.equalTo(self.contentView).offset(-12)
            make.top.equalTo(self.contentView).offset(0)
            make.bottom.equalTo(self.contentView).offset(-10)
        })
        self.containerView?.backgroundColor = .white
        self.containerView?.layer.cornerRadius = 8
        
        self.topLine = UIView()
        self.containerView?.addSubview(self.topLine!)
        self.topLine?.snp.makeConstraints({ make in
            make.top.equalTo(self.containerView!).offset(52)
            make.left.equalTo(self.containerView!).offset(12)
            make.right.equalTo(self.containerView!).offset(-12)
            make.height.equalTo(1)
        })
        self.topLine?.backgroundColor = color("#F5F6F8")
        
        self.storeNameLabel = UILabel()
        self.containerView?.addSubview(self.storeNameLabel!)
        self.storeNameLabel?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerView!).offset(12)
            make.top.equalTo(self.containerView!)
            make.bottom.equalTo(self.topLine!.snp.top)
        })
        self.storeNameLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 16, weight: .semibold), textColor: color("#333333"))
        self.storeNameLabel?.text = "三生有幸茶艺工艺"

        self.statusLabel = UILabel()
        self.containerView?.addSubview(self.statusLabel!)
        self.statusLabel?.snp.makeConstraints({ make in
            make.right.equalTo(self.containerView!).offset(-12)
            make.centerY.equalTo(self.storeNameLabel!)
        })
        self.statusLabel?.labelConfig(textAlignment: .right, font: UIFont.systemFont(ofSize: 14, weight: .regular), textColor: color("#333333"))
        self.statusLabel?.text = "5分钟后关闭"

        self.statusImgV = UIImageView()
        self.containerView?.addSubview(self.statusImgV!)
        self.statusImgV?.snp.makeConstraints({ make in
            make.centerY.equalTo(self.statusLabel!)
            make.right.equalTo(self.statusLabel!.snp.left).offset(-6)
            make.size.equalTo(CGSizeMake(16, 16))
        })
        self.statusImgV?.isHidden = true
        
//        self.goodsOneView = JDOrderGoodsView()
//        self.containerView?.addSubview(self.goodsOneView!)
//        self.goodsOneView?.snp.makeConstraints({ make in
//            make.left.equalTo(self.containerView!)
//            make.right.equalTo(self.containerView!)
//            make.top.equalTo(self.topLine!.snp.bottom)
//            make.height.equalTo(104)
//        })
//
//
//        self.goodsTwoView = JDOrderGoodsView()
//        self.containerView?.addSubview(self.goodsTwoView!)
//        self.goodsTwoView?.snp.makeConstraints({ make in
//            make.left.equalTo(self.containerView!)
//            make.right.equalTo(self.containerView!)
//            make.top.equalTo(self.goodsOneView!.snp.bottom)
//            make.height.equalTo(104)
//        })
        self.goodsTableView = UITableView(frame: .zero, style: .plain)
        self.containerView?.addSubview(self.goodsTableView!)
        self.goodsTableView?.snp.makeConstraints({ make in
            make.left.right.equalTo(self.containerView!)
            make.top.equalTo(self.topLine!.snp.bottom)
            make.height.equalTo(0)
        })
        self.goodsTableView?.separatorStyle = .none
        self.goodsTableView?.delegate = self
        self.goodsTableView?.dataSource = self
        self.goodsTableView?.register(UITableViewCell.self, forCellReuseIdentifier: "goodsCell")
        self.goodsTableView?.isUserInteractionEnabled = false
        
        
        self.waittingPayHintLabel = JDPaddingLabel()
        self.containerView?.addSubview(self.waittingPayHintLabel!)
        self.waittingPayHintLabel?.snp.makeConstraints { make in
            make.left.equalTo(self.containerView!).offset(12)
            make.right.equalTo(self.containerView!).offset(-12)
            make.top.equalTo(self.goodsTableView!.snp.bottom)
            make.height.equalTo(0)
        }
        self.waittingPayHintLabel?.edgeInsets = .init(top: 0, left: 12, bottom: 0, right: 0)
        self.waittingPayHintLabel?.backgroundColor = color("#FFF9F9")
        self.waittingPayHintLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 12, weight: .regular), textColor: color("#FA6161"), numberOfLines: 1)
        self.waittingPayHintLabel?.text = " 订单将于15分钟后自动取消，请买家尽快完成付款。"
        self.waittingPayHintLabel?.appendImgV("order_hint_image", at: 0)
        
        self.bottomLine = UIView()
        self.containerView?.addSubview(self.bottomLine!)
        self.bottomLine?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerView!).offset(12)
            make.right.equalTo(self.containerView!).offset(-12)
            make.height.equalTo(1)
//            make.top.equalTo(self.goodsTableView!.snp.bottom).offset(57)
            make.top.equalTo(self.waittingPayHintLabel!.snp.bottom).offset(57)
            make.bottom.equalTo(self.containerView!).offset(-63)
        })
        self.bottomLine?.backgroundColor = color("#F5F6F8")
        
        self.tagLabel = UILabel()
        self.containerView?.addSubview(self.tagLabel!)
        self.tagLabel?.snp.makeConstraints({ make in
            make.right.equalTo(self.containerView!)
//            make.bottom.equalTo(self.bottomLine!.snp.top).offset(-13)
            make.bottom.equalTo(self.bottomLine!.snp.top).offset(-16)
            make.size.equalTo(CGSize(width: 80, height: 24))
        })
        self.tagLabel?.backgroundColor = UIColor.horizontalGradientColor(leftColor: color("#FA6161"), rightColor: color("#FA8061"), colorFrame: CGRect.init(x: 0, y: 0, width: 80, height: 24))
        self.tagLabel?.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 12, weight: .regular), textColor: .white, numberOfLines: 0)
        self.tagLabel?.text = "跨境直邮"
        self.tagLabel?.makeBezierPathCorner(UIRectCorner(rawValue: UIRectCorner.topLeft.rawValue|UIRectCorner.bottomLeft.rawValue), 12)
        
        self.orderPriceLabel = UILabel()
        self.containerView?.addSubview(self.orderPriceLabel!)
        self.orderPriceLabel?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerView!).offset(12)
//            make.top.equalTo(self.goodsTableView!.snp.bottom).offset(15)
            make.top.equalTo(self.waittingPayHintLabel!.snp.bottom).offset(15)
//            make.height.equalTo(28)
        })
        self.orderPriceLabel?.text = "含税合计付款：￥1200.00"
        self.orderPriceLabel?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 16, weight: .regular), textColor: color("#FA6161"))
        
//        self.orderPriceTip = UILabel()
//        self.containerView?.addSubview(self.orderPriceTip!)
//        self.orderPriceTip?.snp.makeConstraints({ make in
//            make.left.equalTo(self.orderPriceLabel!).offset(-6)
//            make.top.equalTo(self.orderPriceLabel!.snp.bottom).offset(5)
//            make.height.equalTo(17)
//        })
//        self.orderPriceTip?.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 12, weight: .regular), textColor: color("#9DA1B2"))
//        self.orderPriceTip?.text = "（包含关税、跨境物流费、境内物流费）"
        
        self.bottomFunctionView = UIView()
        self.containerView?.addSubview(self.bottomFunctionView!)
        self.bottomFunctionView?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerView!)
            make.right.equalTo(self.containerView!)
            make.top.equalTo(self.bottomLine!.snp.bottom)
            make.bottom.equalTo(self.containerView!)
        })
        
        self.payButton = UIButton(type: .custom)
        self.cancelButton = UIButton(type: .custom)
        self.moreButton = UIButton(type: .custom)
    }
    
    func custonFunctionContent(orderType: JDOrderType) {
        
        self.payButton?.snp.removeConstraints()
        self.cancelButton?.snp.removeConstraints()
        self.moreButton?.snp.removeConstraints()
        
        if orderType == .waitingPay {
            self.waittingPayHintLabel?.snp.updateConstraints { make in
                make.height.equalTo(30)
            }
            
            
            self.bottomFunctionView?.addSubview(self.payButton!)
            self.payButton?.setTitle("支付", for: .normal)
            self.payButton?.setTitleColor(.white, for: .normal)
            self.payButton?.backgroundColor = color("#FA6161")
            self.payButton?.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
            self.payButton?.snp.makeConstraints { make in
                make.centerY.equalTo(self.bottomFunctionView!)
                make.right.equalTo(self.bottomFunctionView!).offset(-12)
                make.size.equalTo(CGSizeMake(82, 40))
            }
            self.payButton?.makeCorner(20)
            
            self.bottomFunctionView?.addSubview(self.cancelButton!)
            self.cancelButton?.setTitle("取消订单", for: .normal)
            self.cancelButton?.setTitleColor(color("#8D9198"), for: .normal)
            self.cancelButton?.backgroundColor = color("#F5F6F8")
            self.cancelButton?.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
            self.cancelButton?.snp.makeConstraints { make in
                make.centerY.equalTo(self.bottomFunctionView!)
                make.right.equalTo(self.payButton!.snp.left).offset(-10)
                make.size.equalTo(CGSizeMake(82, 40))
            }
            self.cancelButton?.makeCorner(20)
            
            self.statusLabel?.text = "等待买家付款"
            self.statusImgV?.isHidden = false
            self.statusImgV?.image = UIImage(named: "waittingPayStatus")?.withRenderingMode(.alwaysOriginal)

            self.waittingPayHintLabel?.snp.updateConstraints { make in
                make.height.equalTo(30)
            }
            
            self.bottomFunctionView?.addSubview(self.moreButton!)
            self.moreButton?.setTitle("更多", for: .normal)
            self.moreButton?.buttonConfig(normalTitle: "更多", normalImageNamed: "", normalTitleColor: color("#333333"), titleFont: UIFont.systemFont(ofSize: 14, weight: .regular), backgroundColor: .clear)
            self.moreButton?.snp.makeConstraints { make in
                make.centerY.equalTo(self.payButton!)
                make.left.equalTo(self.bottomFunctionView!).offset(12)
            }
            
            
        } else if orderType == .alreadyPay {
            self.waittingPayHintLabel?.snp.updateConstraints { make in
                make.height.equalTo(0)
            }
            // 已付款
//            self.cancelButton = UIButton(type: .custom)
            self.bottomFunctionView?.addSubview(self.cancelButton!)
            self.cancelButton?.setTitle("修改地址", for: .normal)
            self.cancelButton?.setTitleColor(color("#8D9198"), for: .normal)
            self.cancelButton?.backgroundColor = color("#F5F6F8")
            self.cancelButton?.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
            self.cancelButton?.snp.makeConstraints { make in
                make.centerY.equalTo(self.bottomFunctionView!)
                make.right.equalTo(self.bottomFunctionView!).offset(-12)
                make.size.equalTo(CGSizeMake(82, 40))
            }
            self.cancelButton?.makeCorner(20)
            
            self.statusLabel?.text = "等待商家发货"
            self.statusImgV?.isHidden = false
            self.statusImgV?.image = UIImage(named: "waittingSend")?.withRenderingMode(.alwaysOriginal)
            
        } else if self.orderType == .sending {
            // 已接单 - 废
           
            
        } else if self.orderType == .pickup {
            self.waittingPayHintLabel?.snp.updateConstraints { make in
                make.height.equalTo(0)
            }
            // 待取货
//            self.cancelButton = UIButton(type: .custom)
            self.bottomFunctionView?.addSubview(self.cancelButton!)
            self.cancelButton?.setTitle("查看物流", for: .normal)
            self.cancelButton?.setTitleColor(.white, for: .normal)
            self.cancelButton?.backgroundColor = color("#8D9198")
            self.cancelButton?.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
            self.cancelButton?.snp.makeConstraints { make in
                make.centerY.equalTo(self.bottomFunctionView!)
                make.right.equalTo(self.bottomFunctionView!).offset(-12)
                make.size.equalTo(CGSizeMake(82, 40))
            }
            self.cancelButton?.makeCorner(20)
            
            self.statusLabel?.text = "商家已发货"
            self.statusImgV?.isHidden = false
            self.statusImgV?.image = UIImage(named: "inTransit")?.withRenderingMode(.alwaysOriginal)
        } else if self.orderType == .completed {
            self.waittingPayHintLabel?.snp.updateConstraints { make in
                make.height.equalTo(0)
            }
            // 已完成 
//            self.cancelButton = UIButton(type: .custom)
            self.bottomFunctionView?.addSubview(self.cancelButton!)
            self.cancelButton?.setTitle("查看物流", for: .normal)
            self.cancelButton?.setTitleColor(.white, for: .normal)
            self.cancelButton?.backgroundColor = color("#8D9198")
            self.cancelButton?.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
            self.cancelButton?.snp.makeConstraints { make in
                make.centerY.equalTo(self.bottomFunctionView!)
                make.right.equalTo(self.bottomFunctionView!).offset(-12)
                make.size.equalTo(CGSizeMake(82, 40))
            }
            self.cancelButton?.makeCorner(20)
            
            self.statusLabel?.text = "已收货"
            self.statusImgV?.isHidden = false
//            self.statusImgV?.backgroundColor = color("#FA6161")
            self.statusImgV?.image = UIImage(named: "gotStatus")?.withRenderingMode(.alwaysOriginal)
        } else if self.orderType == .cancel {
            // 取消订单
//            self.bottomLine?.snp.updateConstraints({ make in
//                make.bottom.equalTo(self.containerView!).offset(0)
//            })
//            self.bottomFunctionView?.isHidden = true
            self.statusLabel?.text = "取消订单"
            self.statusImgV?.isHidden = false
            self.statusLabel?.textColor = color("#9DA1B2")
            self.statusImgV?.image = UIImage(named: "gotStatus")?.withRenderingMode(.alwaysOriginal)
            
        } else if self.orderType == .refusse {
            self.waittingPayHintLabel?.text = "拒单理由:该商品暂无货物，无法正常发货"
            self.waittingPayHintLabel?.snp.updateConstraints { make in
                make.height.equalTo(30)
            }
            // 已拒单
            self.bottomLine?.snp.updateConstraints({ make in
                make.bottom.equalTo(self.containerView!).offset(0)
            })
            self.bottomFunctionView?.isHidden = true
            self.statusLabel?.text = "已拒单"
            self.statusImgV?.isHidden = false
            self.statusLabel?.textColor = color("#9DA1B2")
            self.statusImgV?.image = UIImage(named: "refuseStatus")?.withRenderingMode(.alwaysOriginal)
        }
        
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func refreshData() {
        
        self.custonFunctionContent(orderType: self.orderType)
        self.goodsTableView?.snp.updateConstraints({ make in
            make.height.equalTo((self.orderModel?.goodsList?.count ?? 0)*104)
        })
        
        self.goodsTableView?.reloadData()
        
        self.storeNameLabel?.text = self.orderModel?.shopName
        
        let priceStr = String.init(format: "含税合计付款：￥%.2f", JSON(self.orderModel?.totalAmount as Any).doubleValue/1000.0)
        
        self.orderPriceLabel?.attributedText = priceStr.attributed(firstStr: "含税合计付款：", first: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16, weight: .regular),NSAttributedString.Key.foregroundColor: color("#333333")], secondStr: "￥", second: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12, weight: .regular), NSAttributedString.Key.foregroundColor: color("#FA6161")], thirdStr: String(format: "%.2f", JSON(self.orderModel?.totalAmount as Any).doubleValue/1000.0), third: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20, weight: .semibold),NSAttributedString.Key.foregroundColor: color("#FA6161")])
        
        
        if self.orderModel?.orderStatus == 6 {
            self.statusLabel?.text = "已拒单"
            self.statusImgV?.isHidden = false
            self.statusLabel?.textColor = color("#9DA1B2")
            self.statusImgV?.image = UIImage(named: "refuseStatus")?.withRenderingMode(.alwaysOriginal)
        }
        
    }
}

extension JDOrderTableViewCell: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.orderModel?.goodsList?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "goodsCell")
        
        for subV in (cell?.contentView.subviews ?? [UIView]()) {
            subV.removeFromSuperview()
        }

        cell?.selectionStyle = .none
        
        let currentModel = self.orderModel?.goodsList?[indexPath.row]
        
        let goodsView = JDOrderGoodsView()
        cell?.contentView.addSubview(goodsView)
        goodsView.snp.makeConstraints { make in
            make.left.right.top.bottom.equalTo(cell!.contentView)
        }
        if currentModel?.goodsImgUrl?.hasPrefix("http") ?? false {
            goodsView.goodsImgV.kf.setImage(with: URL(string: currentModel?.goodsImgUrl ?? ""))
        } else {
            goodsView.goodsImgV.downloadImage(currentModel?.goodsImgUrl ?? "")
        }
        
        goodsView.goodsName.text = currentModel?.goodsName
        goodsView.goodsSpec.text = String.init(format: "规格：%@", currentModel?.specificationName ?? "默认")
        goodsView.goodsAddress.text = String.init(format: "产地：%@", currentModel?.origin ?? "")
        goodsView.goodsNum.text = String.init(format: "x %zd", currentModel?.count ?? 0)
        goodsView.goodsPrice.text = String.init(format: "￥%.2f", JSON(currentModel?.goodsPrice as Any).doubleValue/1000.0)
        goodsView.goodsPrice.priceString(UIFont.systemFont(ofSize: 12, weight: .regular), UIFont.systemFont(ofSize: 16, weight: .semibold))
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 104
    }
}





class JDOrderGoodsView: UIView {
// screenWidth *104
    
    var goodsImgV = UIImageView()
    var goodsName = UILabel()
    var goodsSpec = UILabel()
    var goodsAddress = UILabel()
    var goodsPrice = UILabel()
    var goodsNum = UILabel()
    var bottomLine = UIView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupUIElements()
    }
    
    func setupUIElements() {
        
        self.goodsImgV = UIImageView()
        self.addSubview(self.goodsImgV)
        self.goodsImgV.snp.makeConstraints { make in
            make.left.equalTo(self).offset(12)
            make.centerY.equalTo(self)
            make.size.equalTo(CGSize(width: 72, height: 72))
        }
        self.goodsImgV.image = UIImage(named: "loading_placeholder")?.withRenderingMode(.alwaysOriginal)
        self.goodsImgV.contentMode = .scaleAspectFit

        self.addSubview(self.goodsName)
        self.goodsName.snp.makeConstraints { make in
            make.left.equalTo(self.goodsImgV.snp.right).offset(15)
            make.top.equalTo(self.goodsImgV)
            make.right.equalTo(self).offset(-12)
            make.height.equalTo(22)
        }
        self.goodsName.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 16, weight: .regular), textColor: color("#333333"), numberOfLines: 1)
        self.goodsName.text = "「上下一白」羊脂玉瓷素烧茶杯酒杯…"
        
        self.addSubview(self.goodsSpec)
        self.goodsSpec.snp.makeConstraints { make in
            make.left.equalTo(self.goodsName)
            make.top.equalTo(self.goodsName.snp.bottom).offset(4)
            make.height.equalTo(17)
        }
        self.goodsSpec.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 12, weight: .regular), textColor: color("#333333"))
        self.goodsSpec.text = "规格：白色"
        
        self.addSubview(self.goodsAddress)
        self.goodsAddress.snp.makeConstraints { make in
            make.right.equalTo(self).offset(-12)
            make.centerY.equalTo(self.goodsSpec)
        }
        self.goodsAddress.labelConfig(textAlignment: .right, font: UIFont.systemFont(ofSize: 12, weight: .regular), textColor: color("#333333"))
        self.goodsAddress.text = "产地：日本"
        
        self.addSubview(self.goodsPrice)
        self.goodsPrice.snp.makeConstraints { make in
            make.left.equalTo(goodsImgV.snp.right).offset(16)
            make.bottom.equalTo(self.goodsImgV)
            make.height.equalTo(19)
        }
        self.goodsPrice.textColor = color("#333333")
//        self.goodsPrice.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 12, weight: .regular), textColor: color("#333333"))
//        self.goodsPrice.text = "￥700.00"
        
        self.addSubview(self.goodsNum)
        self.goodsNum.snp.makeConstraints { make in
//            make.left.equalTo(self.goodsSpec)
            make.right.equalTo(self).offset(-12)
            make.bottom.equalTo(self.goodsPrice).offset(3)
            make.height.equalTo(17)
        }
        self.goodsNum.labelConfig(textAlignment: .right, font: UIFont.systemFont(ofSize: 12, weight: .regular), textColor: color("#333333"))
        self.goodsNum.text = "x 1"
        
        self.addSubview(self.bottomLine)
        self.bottomLine.snp.makeConstraints { make in
            make.left.equalTo(self).offset(12)
            make.right.equalTo(self).offset(-12)
            make.bottom.equalTo(self)
            make.height.equalTo(1)
        }
        self.bottomLine.backgroundColor = color("#F5F6F8")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension String {
    func attributed(firstStr: String, first: [NSAttributedString.Key : Any], secondStr: String, second: [NSAttributedString.Key : Any], thirdStr: String, third: [NSAttributedString.Key : Any]) -> NSMutableAttributedString {
        
        let attributedStr = NSMutableAttributedString.init(string: self)
        attributedStr.beginEditing()
        
        if let firstRange = self.findAllIndex(firstStr).first {
            attributedStr.addAttributes(first, range: firstRange)
        }

        if let secondRange = self.findAllIndex(secondStr).first {
            attributedStr.addAttributes(second, range: secondRange)
        }
        
        if let thirdRange = self.findAllIndex(thirdStr).first {
            attributedStr.addAttributes(third, range: thirdRange)
        }
        attributedStr.endEditing()
        return attributedStr
    }
    
}
